#include "location_search.h"
#include <QDebug>

// 字符匹配函数，返回与输入匹配的地点名称列表
QVector<QString> searchLocations(const QString& query, const QVector<QString>& locations) {
    QVector<QString> matchingLocations;
    for (const QString& location : locations) {
        if (location.contains(query, Qt::CaseInsensitive)) {
            matchingLocations.append(location);
        }
    }
    return matchingLocations;
}

// 递减匹配函数，当完全匹配不到时，逐步减少输入长度进行匹配
QVector<QString> searchWithPartialMatching(QString query, const QVector<QString>& locations) {
    QVector<QString> results;
    while (query.length() > 0) {
        results = searchLocations(query, locations);
        if (!results.isEmpty()) {
            break;
        }
        query.chop(1);  // 每次减少输入字符串的最后一个字符
    }
    return results;
}

// 结合搜索和判断逻辑的函数
void findAndDisplayLocations(const QString& query, const QVector<QString>& locations) {
    QVector<QString> results = searchLocations(query, locations);

    if (!results.isEmpty()) {
        qDebug() << "Matching locations for" << query << ":";
        for (const QString& result : results) {
            qDebug() << result;
        }
    } else {
        // 如果没有完全匹配的结果，寻找包含部分输入的地点名称
        QVector<QString> partialResults = searchWithPartialMatching(query, locations);
        if (!partialResults.isEmpty()) {
            qDebug() << "Partial matching locations for" << query << ":";
            for (const QString& partialResult : partialResults) {
                qDebug() << partialResult;
            }
        } else {
            qDebug() << "对不起，没有相关景点信息。";
        }
    }
}
